package com.mdd.admin.service.session.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.query.MPJQueryWrapper;
import com.mdd.admin.service.session.IUserSessionService;
import com.mdd.admin.validate.common.PageParam;
import com.mdd.admin.validate.session.UserSessionParam;
import com.mdd.admin.vo.session.UserSessionListVo;
import com.mdd.admin.vo.session.UserSessionDetailVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.session.UserSession;
import com.mdd.common.mapper.session.UserSessionMapper;
import com.mdd.common.utils.ArrayUtil;
import com.mdd.common.utils.TimeUtil;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.config.GlobalConfig;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.*;

/**
 * 用户会话实现类
 */
@Service
public class UserSessionServiceImpl implements IUserSessionService {
        
    @Resource
    UserSessionMapper userSessionMapper;

    /**
     * 用户会话列表
     *
     * @param pageParam 分页参数
     * @param params 搜索参数
     * @return PageResult<UserSessionListVo>
     */
    @Override
    public PageResult<UserSessionListVo> list(PageParam pageParam, Map<String, String> params) {
        Integer page  = pageParam.getPageNo();
        Integer limit = pageParam.getPageSize();

        QueryWrapper<UserSession> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");

        userSessionMapper.setSearch(queryWrapper, params, new String[]{
            "=:userId@user_id:long",
            "=:terminal:int",
            "=:token:str",
            "=:expireTime@expire_time:int",
        });

        IPage<UserSession> iPage = userSessionMapper.selectPage(new Page<>(page, limit), queryWrapper);

        List<UserSessionListVo> list = new LinkedList<>();
        for(UserSession item : iPage.getRecords()) {
            UserSessionListVo vo = new UserSessionListVo();
            BeanUtils.copyProperties(item, vo);
            vo.setUpdateTime(TimeUtil.timestampToDate(item.getUpdateTime()));
            list.add(vo);
        }

        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    /**
     * 用户会话详情
     *
     * @param id 主键参数
     * @return UserSession
     */
    @Override
    public UserSessionDetailVo detail(Integer id) {
        UserSession model = userSessionMapper.selectOne(
                new QueryWrapper<UserSession>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        UserSessionDetailVo vo = new UserSessionDetailVo();
        BeanUtils.copyProperties(model, vo);
        return vo;
    }

    /**
     * 用户会话新增
     *
     * @param userSessionParam 参数
     */
    @Override
    public void add(UserSessionParam userSessionParam) {
        UserSession model = new UserSession();
        model.setUserId(userSessionParam.getUserId());
        model.setTerminal(userSessionParam.getTerminal());
        model.setToken(userSessionParam.getToken());
        model.setExpireTime(userSessionParam.getExpireTime());
        userSessionMapper.insert(model);
    }

    /**
     * 用户会话编辑
     *
     * @param userSessionParam 参数
     */
    @Override
    public void edit(UserSessionParam userSessionParam) {
        UserSession model = userSessionMapper.selectOne(
                new QueryWrapper<UserSession>()
                    .eq("id",  userSessionParam.getId())
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(userSessionParam.getId());
        model.setUserId(userSessionParam.getUserId());
        model.setTerminal(userSessionParam.getTerminal());
        model.setToken(userSessionParam.getToken());
        model.setExpireTime(userSessionParam.getExpireTime());
        userSessionMapper.updateById(model);
    }

    /**
     * 用户会话删除
     *
     * @param id 主键ID
     */
    @Override
    public void del(Integer id) {
        UserSession model = userSessionMapper.selectOne(
                new QueryWrapper<UserSession>()
                    .eq("id", id)
                    .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        userSessionMapper.delete(new QueryWrapper<UserSession>().eq("id", id));
    }

}
